home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GSCOLOR.C < prev    next >
C/C++ Source or Header  |  1992-02-27  |  13KB  |  416 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gscolor.c */
  21. /* Color and halftone operators for GhostScript library */
  22. #include "memory_.h"
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"            /* ditto */
  26. #include "gxmatrix.h"            /* for gzstate.h */
  27. #include "gxdevice.h"            /* for gx_color_index */
  28. #include "gzstate.h"
  29. #include "gzcolor.h"
  30. #include "gzht.h"
  31.  
  32. /* Halftone enumeration structure */
  33. struct gs_screen_enum_s {
  34.     halftone_params ht;        /* constructed here */
  35.     gs_matrix mat;        /* for mapping device x,y to rotated cell */
  36.     int x, y;
  37.     gs_state *pgs;
  38. };
  39.  
  40. /* Exported values */
  41. const uint gs_screen_enum_sizeof = sizeof(gs_screen_enum);
  42. const uint gs_color_sizeof = sizeof(gs_color);
  43.  
  44. /* Forward declarations */
  45. private void tri_param(P4(floatp, floatp, floatp, color_param [3]));
  46. private void tri_return(P4(color_param, color_param, color_param, float [3]));
  47. private void set_phase(P1(gs_state *));
  48. private void load_transfer_map(P2(gs_state *, gx_transfer_map *));
  49. private int unshare_transfer(P1(gs_state *));
  50. /* Imported from gxcolor.c */
  51. void    gx_color_from_hsb(P4(gs_color *, color_param, color_param, color_param)),
  52.     gx_color_from_rgb(P1(gs_color *)),
  53.     gx_color_to_hsb(P2(gs_color *, color_param [3])),
  54.     gx_sort_ht_order(P2(ht_bit *, uint));
  55.  
  56. /* setgray */
  57. int
  58. gs_setgray(gs_state *pgs, floatp gray)
  59. {    if ( pgs->in_cachedevice ) return_error(gs_error_undefined);
  60.     gx_set_gray_only(pgs->color, gx_color_unit_param(gray));
  61.     return gx_remap_color(pgs);
  62. }
  63.  
  64. /* currentgray */
  65. float
  66. gs_currentgray(gs_state *pgs)
  67. {    return (float)(color_luminance(pgs->color) / max_color_param_float);
  68. }
  69.  
  70. /* sethsbcolor */
  71. int
  72. gs_sethsbcolor(gs_state *pgs, floatp h, floatp s, floatp b)
  73. {    color_param params[3];
  74.     if ( pgs->in_cachedevice ) return_error(gs_error_undefined);
  75.     tri_param(h, s, b, params);
  76.     gx_color_from_hsb(pgs->color, params[0], params[1], params[2]);
  77.     return gx_remap_color(pgs);
  78. }
  79.  
  80. /* currenthsbcolor */
  81. int
  82. gs_currenthsbcolor(gs_state *pgs, float pr3[3])
  83. {    color_param hsb[3];
  84.     gx_color_to_hsb(pgs->color, hsb);
  85.     tri_return(hsb[0], hsb[1], hsb[2], pr3);
  86.     return 0;
  87. }
  88.  
  89. /* setrgbcolor */
  90. int
  91. gs_setrgbcolor(gs_state *pgs, floatp r, floatp g, floatp b)
  92. {    if ( pgs->in_cachedevice ) return_error(gs_error_undefined);
  93.     gx_set_rgb_only(pgs->color, gx_color_unit_param(r),
  94.             gx_color_unit_param(g), gx_color_unit_param(b));
  95.     return gx_remap_color(pgs);
  96. }
  97.  
  98. /* currentrgbcolor */
  99. int
  100. gs_currentrgbcolor(gs_state *pgs, float pr3[3])
  101. {    gs_color *pcolor = pgs->color;
  102.     tri_return(pcolor->red, pcolor->green, pcolor->blue, pr3);
  103.     return 0;
  104. }
  105.  
  106. /* currentcolorspace */
  107. int
  108. gs_currentcolorspace(gs_state *pgs, gs_color_space *pcs)
  109. {    *pcs = (gs_color_space)pgs->color->space;
  110.     return 0;
  111. }
  112.  
  113. /* setscreen */
  114. int
  115. gs_setscreen(gs_state *pgs,
  116.   floatp freq, floatp angle, float (*proc)(P2(floatp, floatp)))
  117. {    gs_screen_enum senum;
  118.     gs_point pt;
  119.     int code = gs_screen_init(&senum, pgs, freq, angle);
  120.     if ( code < 0 ) return code;
  121.     while ( (code = gs_screen_currentpoint(&senum, &pt)) == 0 )
  122.         if ( (code = gs_screen_next(&senum, (*proc)(pt.x, pt.y))) < 0 )
  123.             return code;
  124.     if ( code < 0 ) return code;
  125.     pgs->ht_proc = proc;
  126.     set_phase(pgs);
  127.     return 0;
  128. }
  129.  
  130. /* currentscreen */
  131. int
  132. gs_currentscreen(gs_state *pgs,
  133.   float *pfreq, float *pangle, float (**pproc)(P2(floatp, floatp)))
  134. {    halftone_params *pht = pgs->halftone;
  135.     *pfreq = pht->frequency;
  136.     *pangle = pht->angle;
  137.     *pproc = pgs->ht_proc;
  138.     return 0;
  139. }
  140.  
  141. /* settransfer */
  142. /* Remap=0 is used by the interpreter. */
  143. int
  144. gs_settransfer_remap(gs_state *pgs, gs_transfer_proc tproc, int remap)
  145. {    int code = unshare_transfer(pgs);
  146.     gx_transfer *ptran;
  147.     if ( code < 0 ) return code;
  148.     ptran = pgs->transfer;
  149.     ptran->gray.proc = tproc;
  150.     if ( remap )
  151.       load_transfer_map(pgs, &ptran->gray);
  152.     ptran->red = ptran->gray;
  153.     ptran->green = ptran->gray;
  154.     ptran->blue = ptran->gray;
  155.     return (remap ? gx_remap_color(pgs) : 0);
  156. }
  157. int
  158. gs_settransfer(gs_state *pgs, gs_transfer_proc tproc)
  159. {    return gs_settransfer_remap(pgs, tproc, 1);
  160. }
  161.  
  162. /* currenttransfer */
  163. gs_transfer_proc
  164. gs_currenttransfer(gs_state *pgs)
  165. {    return pgs->transfer->gray.proc;
  166. }
  167.  
  168. /* setcolortransfer */
  169. /* Remap=0 is used by the interpreter. */
  170. int
  171. gs_setcolortransfer_remap(gs_state *pgs, gs_transfer_proc red_proc,
  172.   gs_transfer_proc green_proc, gs_transfer_proc blue_proc,
  173.   gs_transfer_proc gray_proc, int remap)
  174. {    int code = unshare_transfer(pgs);
  175.     gx_transfer *ptran;
  176.     if ( code < 0 ) return code;
  177.     ptran = pgs->transfer;
  178.     ptran->red.proc = red_proc;
  179.     ptran->green.proc = green_proc;
  180.     ptran->blue.proc = blue_proc;
  181.     ptran->gray.proc = gray_proc;
  182.     if ( remap )
  183.       { load_transfer_map(pgs, &ptran->red);
  184.         load_transfer_map(pgs, &ptran->green);
  185.         load_transfer_map(pgs, &ptran->blue);
  186.         load_transfer_map(pgs, &ptran->gray);
  187.         return gx_remap_color(pgs);
  188.       }
  189.     else
  190.       return 0;
  191. }
  192. int
  193. gs_setcolortransfer(gs_state *pgs, gs_transfer_proc red_proc,
  194.   gs_transfer_proc green_proc, gs_transfer_proc blue_proc,
  195.   gs_transfer_proc gray_proc)
  196. {    return gs_setcolortransfer_remap(pgs, red_proc, green_proc,
  197.                      blue_proc, gray_proc, 1);
  198. }
  199.  
  200. /* currentcolortransfer */
  201. void
  202. gs_currentcolortransfer(gs_state *pgs, gs_transfer_proc procs[4])
  203. {    gx_transfer *ptran = pgs->transfer;
  204.     procs[0] = ptran->red.proc;
  205.     procs[1] = ptran->green.proc;
  206.     procs[2] = ptran->blue.proc;
  207.     procs[3] = ptran->gray.proc;
  208. }
  209.  
  210. /* sethalftonephase */
  211. int
  212. gs_sethalftonephase(gs_state *pgs, int x, int y)
  213. {    pgs->ht_phase.x = x;
  214.     pgs->ht_phase.y = y;
  215.     set_phase(pgs);
  216.     return 0;
  217. }
  218.  
  219. /* currenthalftonephase */
  220. int
  221. gs_currenthalftonephase(gs_state *pgs, gs_int_point *pphase)
  222. {    *pphase = pgs->ht_phase;
  223.     return 0;
  224. }
  225.  
  226. /* ------ Halftone sampling ------ */
  227.  
  228. /* Set up for halftone sampling */
  229. int
  230. gs_screen_init(gs_screen_enum *penum, gs_state *pgs,
  231.   floatp freq, floatp angle)
  232. {    int cwidth, cheight;
  233.     int code;
  234.     ht_bit *order;
  235.     if ( freq < 0 ) return_error(gs_error_rangecheck);
  236.     /* Convert the frequency to cell width and height */
  237.        {    float cell_size = 72.0 / freq;
  238.         gs_point pcwh;
  239.         gs_matrix imat;
  240.         gs_deviceinitialmatrix(gs_currentdevice(pgs), &imat);
  241.         if ( (code = gs_distance_transform(cell_size, cell_size,
  242.                            &imat, &pcwh)) < 0
  243.             ) return code;
  244.         /* It isn't clear to me whether we should round the */
  245.         /* width and height, truncate them, or do something */
  246.         /* more complicated.  All the problems arise from devices */
  247.         /* whose X and Y resolutions aren't the same: */
  248.         /* the halftone model isn't really designed for this. */
  249.         /* For the moment, truncate and hope for the best. */
  250. #define abs_round(z) (z < 0 ? -(int)(z) : (int)(z))
  251. /*#define abs_round(z) (z < 0 ? -(int)(z - 0.5) : (int)(z + 0.5))*/
  252.         cwidth = abs_round(pcwh.x);
  253.         cheight = abs_round(pcwh.y);
  254. #undef abs_round
  255.        }
  256.     /* Force a halfway reasonable cell size. */
  257.     if ( cwidth <= 4 ) cwidth = 4;
  258.     if ( cheight <= 4 ) cheight = 4;
  259.     if ( cwidth > max_ushort / cheight )
  260.         return_error(gs_error_rangecheck);
  261.     order = (ht_bit *)gs_malloc(cwidth * cheight, sizeof(ht_bit),
  262.                     "halftone samples");
  263.     if ( order == 0 ) return_error(gs_error_VMerror);
  264.     penum->ht.frequency = freq;
  265.     penum->ht.angle = angle;
  266.     penum->ht.order = order;
  267.     penum->ht.width = cwidth;
  268.     penum->ht.height = cheight;
  269.     penum->ht.order_size = cwidth * cheight;
  270.     penum->x = penum->y = 0;
  271.     penum->pgs = pgs;
  272.     /* The transformation matrix must include normalization to the */
  273.     /* interval (-1..1), and rotation by the negative of the angle. */
  274.        {    float xscale = 2.0 / cwidth;
  275.         float yscale = 2.0 / cheight;
  276.         gs_matrix mat;
  277.         gs_make_identity(&mat);
  278.         mat.xx = xscale;
  279.         mat.yy = yscale;
  280.         mat.tx = xscale * 0.5 - 1.0;
  281.         mat.ty = yscale * 0.5 - 1.0;
  282.         if ( (code = gs_matrix_rotate(&mat, -angle, &penum->mat)) < 0 )
  283.             return code;
  284. #ifdef DEBUG
  285. if ( gs_debug['h'] )
  286.     dprintf8("[h]Screen: w=%d h=%d [%f %f %f %f %f %f]\n",
  287.          cwidth, cheight, penum->mat.xx, penum->mat.xy,
  288.          penum->mat.yx, penum->mat.yy, penum->mat.tx, penum->mat.ty);
  289. #endif
  290.        }
  291.     return 0;
  292. }
  293.  
  294. /* Report current point for sampling */
  295. private int gx_screen_finish(P1(gs_screen_enum *));
  296. int
  297. gs_screen_currentpoint(gs_screen_enum *penum, gs_point *ppt)
  298. {    gs_point pt;
  299.     int code;
  300.     if ( penum->y >= penum->ht.height )    /* all done */
  301.         return gx_screen_finish(penum);
  302.     if ( (code = gs_point_transform((floatp)penum->x, (floatp)penum->y, &penum->mat, &pt)) < 0 )
  303.         return code;
  304.     while ( pt.x < -1.0 ) pt.x += 2.0;
  305.     while ( pt.x >= 1.0 ) pt.x -= 2.0;
  306.     while ( pt.y < -1.0 ) pt.y += 2.0;
  307.     while ( pt.y >= 1.0 ) pt.y -= 2.0;
  308.     *ppt = pt;
  309.     return 0;
  310. }
  311.  
  312. /* Record next halftone sample */
  313. int
  314. gs_screen_next(gs_screen_enum *penum, floatp value)
  315. {    ushort sample;
  316.     if ( value < -1.0 || value > 1.0 )
  317.       return_error(gs_error_rangecheck);
  318.     /* The following statement was split into two */
  319.     /* to work around a bug in the Siemens C compiler. */
  320.     sample = (ushort)(value * (float)(int)(max_ushort >> 1));
  321.     sample += (max_ushort >> 1);    /* convert from signed to biased */
  322. #ifdef DEBUG
  323. if ( gs_debug['h'] )
  324.    {    gs_point pt;
  325.     gs_screen_currentpoint(penum, &pt);
  326.     dprintf6("[h]sample x=%d y=%d (%f,%f): %f -> %u\n",
  327.          penum->x, penum->y, pt.x, pt.y, value, sample);
  328.    }
  329. #endif
  330.     penum->ht.order[penum->y * penum->ht.width + penum->x].mask = sample;
  331.     if ( ++(penum->x) >= penum->ht.width )
  332.         penum->x = 0, ++(penum->y);
  333.     return 0;
  334. }
  335.  
  336. /* All points have been sampled. */
  337. /* Finish constructing the halftone. */
  338. private int
  339. gx_screen_finish(gs_screen_enum *penum)
  340. {    ht_bit *order = penum->ht.order;
  341.     uint size = penum->ht.order_size;
  342.     uint i;
  343.     int code;
  344.     /* Label each element with its ordinal position. */
  345.     for ( i = 0; i < size; i++ )
  346.         order[i].offset = i;
  347.     /* Sort the samples in increasing order by value. */
  348.     gx_sort_ht_order(order, size);
  349.     /* Set up the actual halftone description. */
  350.     code = gx_ht_construct_order(order, penum->ht.width, penum->ht.height);
  351.     if ( code < 0 ) return code;
  352.     *penum->pgs->halftone = penum->ht;
  353.     return 1;            /* all done */
  354. }
  355.  
  356. /* ------ Internal routines ------ */
  357.  
  358. /* Get 3 real parameters in the range [0..1], */
  359. /* and convert them to color_params. */
  360. private void
  361. tri_param(floatp p1, floatp p2, floatp p3, color_param pq3[3])
  362. {    pq3[0] = gx_color_unit_param(p1);
  363.     pq3[1] = gx_color_unit_param(p2);
  364.     pq3[2] = gx_color_unit_param(p3);
  365. }
  366.  
  367. /* Convert 3 color_params to reals */
  368. private void
  369. tri_return(color_param p1, color_param p2, color_param p3, float pr3[3])
  370. {    pr3[0] = p1 / max_color_param_float;
  371.     pr3[1] = p2 / max_color_param_float;
  372.     pr3[2] = p3 / max_color_param_float;
  373. }
  374.  
  375. /* Compute the negated halftone phase mod the tile size. */
  376. /* This is the displacement of the tile relative to the device coordinates. */
  377. private void
  378. set_phase(gs_state *pgs)
  379. {    halftone_params *pht = pgs->halftone;
  380.     if ( pht->width == 0 )
  381.         pgs->phase_mod.x = 0;
  382.     else
  383.        {    if ( (pgs->phase_mod.x = -pgs->ht_phase.x % pht->width) < 0 )
  384.             pgs->phase_mod.x += pht->width;
  385.        }
  386.     if ( pht->height == 0 )
  387.         pgs->phase_mod.y = 0;
  388.     else
  389.        {    if ( (pgs->phase_mod.y = -pgs->ht_phase.y % pht->height) < 0 )
  390.             pgs->phase_mod.y += pht->height;
  391.        }
  392. }
  393.  
  394. /* Load one cached transfer map. */
  395. private void
  396. load_transfer_map(gs_state *pgs, gx_transfer_map *pmap)
  397. {    gs_transfer_proc proc = pmap->proc;
  398.     color_param *values = pmap->values;
  399.     int i;
  400.     for ( i = 0; i < transfer_map_size; i++ )
  401.       values[i] = gx_color_unit_param((*proc)(pgs, (float)i / transfer_map_size));
  402. }
  403.  
  404. /* Ensure that the transfer map is not shared. */
  405. private int
  406. unshare_transfer(gs_state *pgs)
  407. {    if ( pgs->transfer->ref_count > 1 )
  408.        {    gx_transfer *ptran = (gx_transfer *)(*pgs->memory_procs.alloc)(1, sizeof(gx_transfer), "unshare_transfer");
  409.         if ( ptran == 0 ) return_error(gs_error_VMerror);
  410.         ptran->ref_count = 1;
  411.         pgs->transfer->ref_count--;
  412.         pgs->transfer = ptran;
  413.        }
  414.     return 0;
  415. }
  416.